package com.college.banner.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.college.banner.entity.CrmBanner;
import com.college.banner.entity.vo.BannerQuery;
import com.college.banner.service.CrmBannerService;
import com.college.commonutil.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cmsservice/bannerAdmin")
@CrossOrigin //解决跨域问题
public class CrmBannerController {
        @Autowired
        private CrmBannerService crmBannerService;

        //条件分页查询banner
        @PostMapping("/pageBanner/{page}/{limit}")
        public Result pageBanner(@PathVariable Long page, @PathVariable Long limit, @RequestBody(required = false) BannerQuery bannerQuery){
            Page<CrmBanner> bannerPage = new Page<>(page,limit);

            crmBannerService.pageQuery(bannerPage,bannerQuery);

            //获取数据
            List<CrmBanner> list = bannerPage.getRecords();
            //获取总记录数
            long total = bannerPage.getTotal();

            return Result.ok().data("rows",list).data("total",total);
        }

        //添加banner
        @PostMapping("/addBanner")
        public Result addBanner(@RequestBody CrmBanner crmBanner){
            crmBannerService.saveBanner(crmBanner);
                return Result.ok();
        }

        //修改banner
        @PostMapping("/updateBanner")
        public Result updateBanner(@RequestBody CrmBanner crmBanner){
             crmBannerService.updateBannerById(crmBanner);
                return Result.ok();
        }

        //根据id删除banner
        @DeleteMapping("/deleteBannerById/{id}")
        public Result deleteBannerById(@PathVariable String id){
            crmBannerService.removeBannerById(id);
                return Result.ok();
        }

        //根据id查询banner
        @GetMapping("/getBannerById/{id}")
        public Result getBannerById(@PathVariable String id){
            CrmBanner crmBanner = crmBannerService.getById(id);
            return Result.ok().data("item",crmBanner);
        }
}
